<%@ include file="/includes/taglibs.jsp"%>
<ul class="breadcrumb">
	<li><a href="<c:url value="/usergroups/list.html"/>">Usergroups</a> <span
		class="divider">/</span></li>
	<li class="active">Add or Update data</li>
</ul>

<H2>Data User</H2>
<c:if test="${fn:length(errorMsg) ne 0}">
	<div class="well">
		<h4>
			<img src="<c:url value="/images/warning.gif"/>" align="top" /> There
			was errors!
		</h4>
		<ol>
			<c:forEach items="${errorMsg}" var="error">
				<li>${error}</li>
			</c:forEach>
		</ol>

	</div>
</c:if>

<form method="post" class="form-horizontal" style="padding: 5px;" name="form-data" novalidate="novalidate">
	<input type="hidden" id="usergroupid" name="usergroupid"/>
	<div class="control-group">
		<label class="control-label" for="usergroupname">Usergroup Name*</label>
		<div class="controls">
			<div class="input-prepend">				
				<input type="text" id="usergroupname" name="usergroupname" placeholder="Usergroup Name" required/>
			</div>
		</div>
	</div>	
	<div class="control-group">	
		<button type="submit" class="btn btn-primary">Save</button>
		<button type="reset" class="btn">Reset</button>
		<button type="button" class="btn"
			onclick="location.href='<c:url value="/usergroups/list.html"/>'">Cancel</button>
	</div>
</form>

<script type="text/javascript">

	$(document).ready(
	function() {		
		function getURLParameter(name) {
			return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(
					location.search) || [ , null ])[1]);
		}

		var itemid = getURLParameter("itemId");
		//console.log("itemid="+itemid);
		if (itemid != null && itemid != 'null') {
			$.getJSON('<c:url value="/usergroups/findone/" />' + itemid,
					function(data) {
						$.each(data, function(obj, value) {							
							$('input[name="' + obj + '"]').val(value);
						});
					});
		}
		$('form[name="form-data"]').submit(function() {
			//console.log($('form[name="form-data"]').valid());
			if($('form[name="form-data"]').valid()){			
				var jsondata = $('form').serializeObject();
				var jsonst = JSON.stringify(jsondata);
				var json = JSON.parse(jsonst);				

				//console.log(JSON.stringify(json));
				$.ajax({
					type : "POST",
					url : "<c:url value="/usergroups/save" />",
					contentType : "application/json; charset=utf-8",
					dataType : "json",
					data : JSON.stringify(json)
				}).done(function(msg) {
					alert("Data Saved: " + msg);
					window.location.href = "<c:url value="/usergroups/list.html"/>";
				});

			return false;
			}
		});
	});
</script>

